package com.health.controller;

import com.health.config.Student;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@RestController
public class TestController {

    @Value("${student.name}")
    private String name;

    @Value("${student.city}")
    private String stuCity;

    @Value("${city[4]}")
    private String city5;

    @Resource
    private Environment environment;

    @Resource
    private Student student;

    @GetMapping("/hello")
    public void hello(HttpServletResponse response) throws IOException {
        //中文编码格式
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.write("name:"+name);
        writer.write("\nstuCity:"+stuCity);
        writer.write("\ncity[4]:"+city5);
    }

    @GetMapping("/hello2")
    public Object hello2(){

        return "stucity:"+stuCity;
    }

    @GetMapping("/hello3")
    public Object hello3(){
        //注意使用environment获取配置属性值时,不用el表达式
        String name = environment.getProperty("student.name");
        String city = environment.getProperty("city[2]");

        return "name:"+name+"--city:"+city;
    }

    @GetMapping("/hello4")
    public Object hello4(){
        System.out.println(student);

        return student.getName()+student.getAge()+student.getCity();
    }

}